package cn.edu.zzuli.controller;

import cn.edu.zzuli.anno.CurrentUser;
import cn.edu.zzuli.entity.R;
import cn.edu.zzuli.service.UserEntityService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
@Slf4j
//@Api(tags = "用户相关接口")
public class UserEntityController {


    @Resource
    UserEntityService userEntityService;

    @GetMapping("/info")
    @ApiOperation("获取当前登录用户的信息")
    public R get(@CurrentUser Integer uId) {
        if (uId == null) return R.error().appendMsg("当前好像没有登录的用户啊");
        return R.success()
                .add("user", userEntityService.getUserById(uId));
    }

    @PostMapping("/login")
//    @ApiOperation(value = "登录接口，返回 token")
    public R login(@RequestBody JSONObject jsonObject) {

        if (!StringUtils.isEmpty(jsonObject.get("username"))
                && !StringUtils.isEmpty(jsonObject.get("password"))) {

            return userEntityService.getUserByUserName(jsonObject.getString("username"),
                    jsonObject.getString("password"));
        }

        return R.error().appendMsg("参数不对");
    }

    @PutMapping("/register")
    public R register(@RequestBody JSONObject jsonObject) {
        return userEntityService.aboutRegister(jsonObject);
    }

}
